package com.bite.springmvc.controller;

import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session) {
//        //参数校验
//        if(userName==null||userName.equals("")) return false;
//        if(StringUtils.hasLength(userName)&&StringUtils.hasLength(password)) {
//            //校验账号密码是否正确
//            //当前还未学习数据库的操作，只能采用硬编码的方式
//            //假设账号密码为张三 123456
//            if("zhangsan".equals(userName)&&"123456".equals(password)) return true;
//        }
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }
        //校验账号和密码   假设 账号为: zhangsan, 密码为123456
        if ("zhangsan".equals(userName) && "123456".equals(password)) {
            //存储Session
            session.setAttribute("userName", userName);
            return true;
        }
        return false;
    }
    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpSession session) {
        String userName = (String) session.getAttribute("userName");
        System.out.println("登录用户："+userName);
        return userName==null?"":userName;
    }
}
